<?php
class User extends BaseDB
{
	private $tokenKey = 'q)*5l;xw9';
	private $saltKey = '#@pe2w3sll';
	function __construct()
	{
		$this->table = 'user';
		parent::__construct();
	}
	
	/**
	 * 添加用户
	 *
	 * @param array $data = array('username', 'password', 'level', 'remark')
	 * @return unknown
	 */
	public function addUser($data)
	{
		if (empty($data['username']) || 
			empty($data['password']) || 
			empty($data['level'])) return false;
		
		$data['salt'] = $this->genSalt();
		$data['password'] = $this->genPassword($data['password'], $data['salt']);
		$data['token'] = $this->genToken($data['username'], $data['password']);
		$data['status'] = $this->conf['status']['enabled'];
		$data['update_time'] = date("Y-m-d H:i:s");
		return $this->insert($data);
	}
	
	public function deleteUserByUserName($username)
	{
		if (empty($username))return false;
		return $this->modifyUserInfo(array('status'=>$userStatus['disabled']), array('username'=>$username));
		return $this->delete(array('username'=>$username));
	}
	
	public function deleteUserById($id)
	{
		if (empty($id))return false;
//		return $this->modifyUserInfo(array('status'=>$userStatus['disabled']), array('id'=>$id));
		return $this->delete(array('id'=>$id));
	}
	
	public function modifyUserInfo($data, $where)
	{
		if (empty($data) || !is_array($data))return false;
		$data['update_time'] = date("Y-m-d H:i:s");
		return $this->update($data, $where);
	}
	
	public function getUserList($start = 0, $count = 20)
	{
		$start = max(0, (int)$start);
		$count = max(20, (int)$count);
		$sql = "select * from " . $this->table . " where level<? and status=? limit " . $start . "," .$count;
		return $this->db->getAll($sql, array(999999, 1));
	}
	
	public function getUserInfoById($id)
	{
		if (empty($id))return false;
		$userInfo = $this->select(array('id'=>$id, 'status'=>$this->conf['status']['enabled']));
		if (!empty($userInfo)){
			return $userInfo[0];
		}
		return false;
	}
	
	public function getUserInfoByUserName($username)
	{
		if (empty($username))return false;
		$userInfo = $this->select(array('username'=>$username, 'status'=>$this->conf['status']['enabled']));
		if (!empty($userInfo)){
			return $userInfo[0];
		}
		return false;
	}
	
	protected function genPassword($password, $salt)
	{
		return md5($password . $salt);
	}
	
	public function checkPassword($username, $password)
	{
		$userInfo = $this->getUserInfoByUserName($username);
		if ($userInfo && $this->genPassword($password, $userInfo['salt']) == $userInfo['password']){
			return $userInfo;
		}
		return false;
	}
	
	public function modifyPassword($username, $password) {
		$userInfo = $this->getUserInfoByUserName($username);
		if (empty($userInfo)) {
			return false;
		}
		
		$newPassword = $this->genPassword($password, $userInfo['salt']);
		return $this->modifyUserInfo(array('password'=>$newPassword), array('username'=>$username));
	}
	
	protected function genToken($username, $password)
	{
		return md5($username . $password . $this->tokenKey);
	}
	
	public function checkToken($id, $token)
	{
		$userInfo = $this->getUserInfoById($id);
		if ($userInfo){
			return ($userInfo['token'] == $token);
		}
		return false;
	}
	
	protected function genSalt()
	{
		return substr(md5(time() . rand(100001,999999) . $this->saltKey), 8, -8);
	}
	
	public function encodeToken($id, $username, $token)
	{
		if (empty($id) || empty($username) || empty($token)) return false;
		return base64_encode('id='.$id.'&user='.$username.'&t='.$token);
	}
	
	public function decodeToken($encodeStr)
	{
		$decodeStr = base64_decode($encodeStr);
		if (!$decodeStr) return false;
		parse_str($decodeStr, $res);
		return $res;
	}
}
?>